function loadPeople()
{
	url='model/ajax/people.php';
	$("#content").load(url,onceLoadedPeople);
}

function onceLoadedPeople()
{
	$(".people-group-menu-button").click
	(
		function()
		{
//			$(".people-group-menu-button").removeClass("li-selected");

			var selectedPeopleGroups = new Array();
			$(this).toggleClass("li-selected");
			$(".li-selected").each(
				function()
				{
					groupID = $(this).attr("id");
					var start = groupID.lastIndexOf("_");
					groupNumber = groupID.slice(start+1);
					selectedPeopleGroups.push(groupNumber);
				}
			);

			url='model/ajax/people-reload.php';
			$.post(
				url,
				{people:selectedPeopleGroups},
				onceLoadedPeopleSet);
		}
	);
		
	$("#people-actions-menu-button-getEmails").click
	(
		function()
		{
//			alert("hi");
			selectedPeopleGroups = getSelectedPeopleGroups();
/*			var sql="";
//			$(this).toggleClass("li-selected");
			$(".li-selected").each
			(
				function()
				{
					groupID = $(this).attr("id");
					var start = groupID.lastIndexOf("_");
					groupNumber = groupID.slice(start+1);
					selectedPeopleGroups.push(groupNumber);
//					alert("groupID: "+groupID);
//					alert("groupnumber: "+groupNumber);
				}
			)*/
			url='model/ajax/people-email.php';
			$.post(
				url,
				{emailGroups:selectedPeopleGroups},
				onceLoadedGetEmails);
		}
	);
	
	$("#people-actions-menu-button-addPerson").click
	(
		function()
		{
			personID = 0;
			url='model/ajax/people-edit.php';
			$.post(
				url,
				{personID:personID},
				onceLoadedEditPerson);			
		}
	);
	
	$(".people-actions-menu-button").hover(function(){$(this).addClass("li-hover");},function(){$(this).removeClass("li-hover");});
	$(".people-group-menu-button").hover(function(){$(this).addClass("li-hover");},function(){$(this).removeClass("li-hover");});
	$("#content").slideDown(2000);

}

function onceLoadedPeopleSet(result)
{
	$("#people").html(result);
	$("#people-window").html("");
	$("#people-edit").html("");
	$("#people-edit").hide();

	$(".people-table").click
	(
		function()
		{
//			alert($(this).attr("id"));
			attrStr=$(this).attr("id");
			personStr=attrStr.split("-");
			personID = personStr[2];
			url='model/ajax/people-edit.php';
			$.post(
				url,
				{personID:personID},
				onceLoadedEditPerson);			
		}
	);
	
	$(".people-table-address").hover
	(
		function()
		{
			$(this).next().show();
		},
		function()
		{
			$(this).next().hide();
			}
		);
	
}

function onceLoadedGetEmails(result)
{
	$("#people-window").html(result);
}

function onceLoadedEditPerson(result)
{
	$("#people-edit").html(result);
	$("#people-edit-addgroup").hide();
	$("#people-edit-addaddress").hide();
	
	$(".removeEmailIcon").click
	(
		function()
		{
//			alert($(this).attr("id"));	
//			alert($("#people-edit-value-id").val());
			url='model/ajax/people-edit-changeemail.php';
			$.post(
				url,
				{
					action:"remove",
					personID:$("#people-edit-value-id").val(),
					email:$(this).attr("id")
				}
				);
			url='model/ajax/people-edit.php';
			$.post(
				url,
				{personID:$("#people-edit-value-id").val()},
				onceLoadedEditPerson);			
			
		}
	);
	
	$(".addEmailIcon").click
	(
		function()
		{
			url='model/ajax/people-edit-changeemail.php';
			$.post(
				url,
				{
					action:"add",
					personID:$("#people-edit-value-id").val(),
					email:$("#people-edit-value-add-email").val()
				}
				);
			url='model/ajax/people-edit.php';
			$.post
			(
				url,
				{personID:$("#people-edit-value-id").val()},
				onceLoadedEditPerson
			);			
			
		}
	);
	
	$(".removeGroupIcon").click
	(
		function()
		{
			url='model/ajax/people-edit-removegroup.php';
			$.post
			(
				url,
				{
					personid:$("#people-edit-value-id").val(),
					groupid:$(this).attr("id")
				}
			);
			
			url='model/ajax/people-reload.php';
			selectedPeopleGroups = getSelectedPeopleGroups();
			$.post(
				url,
				{people:selectedPeopleGroups},
				onceLoadedPeopleSet);

			personID = $("#people-edit-value-id").val();
			url='model/ajax/people-edit.php';
			$.post(
				url,
				{personID:$("#people-edit-value-id").val()},
				onceLoadedEditPerson);			
		}
	);
	
	$(".addGroupIcon").click
	(
		function()
		{
			url='model/ajax/people-edit-addgroup.php';
			$.post
			(
				url,
				{
					personID:$("#people-edit-value-id").val()
				},
				onceLoadedAddGroup
			);
		
		}
	);
	
	$(".removeTelephoneIcon").click
	(
		function()
		{
//			alert($(this).attr("id"));	
//			alert($("#people-edit-value-id").val());
			url='model/ajax/people-edit-changetelephone.php';
			$.post(
				url,
				{
					action:"remove",
					personID:$("#people-edit-value-id").val(),
					telephone:$(this).attr("id")
				}
				);
			url='model/ajax/people-edit.php';
			$.post(
				url,
				{personID:$("#people-edit-value-id").val()},
				onceLoadedEditPerson);			
			
		}
	);
	
	$(".addTelephoneIcon").click
	(
		function()
		{
			url='model/ajax/people-edit-changetelephone.php';
			$.post(
				url,
				{
					action:"add",
					personID:$("#people-edit-value-id").val(),
					telephone:$("#people-edit-value-add-telephone").val()
				}
				);
			url='model/ajax/people-edit.php';
			$.post
			(
				url,
				{personID:$("#people-edit-value-id").val()},
				onceLoadedEditPerson
			);			
			
		}
	);
	
	$(".removeAddressIcon").click
	(
		function()
		{
			url='model/ajax/people-edit-removeaddress.php';
			$.post
			(
				url,
				{
					personid:$("#people-edit-value-id").val(),
					addressid:$(this).attr("id")
				}
			);
			
			url='model/ajax/people-reload.php';
			selectedPeopleGroups = getSelectedPeopleGroups();
			$.post(
				url,
				{people:selectedPeopleGroups},
				onceLoadedPeopleSet);

			personID = $("#people-edit-value-id").val();
			url='model/ajax/people-edit.php';
			$.post(
				url,
				{personID:$("#people-edit-value-id").val()},
				onceLoadedEditPerson);			
		}
	);
	
	$(".addAddressIcon").click
	(
		function()
		{
			url='model/ajax/people-edit-addaddress.php';
			$.post
			(
				url,
				{
					personID:$("#people-edit-value-id").val()
				},
				onceLoadedAddAddress
			);
		
		}
	);
	

	$(".people-edit-menu-button").hover(function(){$(this).addClass("li-hover");},function(){$(this).removeClass("li-hover");});

	$("#people-edit-menu-button-cancel").click(
		function()
		{
			$("#people-edit").html("");
			$("#people-edit").hide();
		});

	$("#people-edit-menu-button-save").click(
		function()
		{
			url='model/ajax/people-save.php';
			$.post
			(
				url,
				{
					personid:$("#people-edit-value-id").val(),
					firstname:$("#people-edit-value-firstname").val(),
					surname:$("#people-edit-value-surname").val(),
				}
			);
			url='model/ajax/people-reload.php';
			selectedPeopleGroups = getSelectedPeopleGroups();
			$.post(
				url,
				{people:selectedPeopleGroups},
				onceLoadedPeopleSet);

		});

	$("#people-edit-menu-button-delete").click(
		function()
		{
			url='model/ajax/people-delete.php';
			$.post
			(
				url,
				{
					personid:$("#people-edit-value-id").val(),
				}
			);
			url='model/ajax/people-reload.php';
			selectedPeopleGroups = getSelectedPeopleGroups();
			$.post(
				url,
				{people:selectedPeopleGroups},
				onceLoadedPeopleSet);

		});


	
	$("#people-edit").show();
}


function onceLoadedAddGroup(result)
{
	$("#people-edit-addgroup").html(result);
	$("#people-edit-addgroup").show();
	
	$(".people-edit-addgroup-group").hover(function(){$(this).addClass("li-hover");},function(){$(this).removeClass("li-hover");});
	$(".people-edit-addgroup-group").click
	(
		function()
		{
			attrStr=$(this).attr("id");
			groupStr=attrStr.split("-");
			groupid = groupStr[4];

			url='model/ajax/people-edit-addgroup-save.php';
			$.post
			(
				url,
				{
					personid:$("#people-edit-value-id").val(),
					groupid:groupid
				}
			);
			url='model/ajax/people-reload.php';
			selectedPeopleGroups = getSelectedPeopleGroups();
			$.post(
				url,
				{people:selectedPeopleGroups},
				onceLoadedPeopleSet);

			url='model/ajax/people-edit.php';
			$.post
			(
				url,
				{personID:$("#people-edit-value-id").val()},
				onceLoadedEditPerson
			);			
		}
	);
	
	
	$("#people-edit-addgroup-cancel").hover(function(){$(this).addClass("but-hover");},function(){$(this).removeClass("but-hover");});
	$("#people-edit-addgroup-cancel").click(function(){$("#people-edit-addgroup").hide();});

}

function onceLoadedAddAddress(result)
{
	$("#people-edit-addaddress").html(result);
	$("#people-edit-addaddress").show();
	
	$(".people-edit-menu-button").hover(function(){$(this).addClass("li-hover");},function(){$(this).removeClass("li-hover");});
	$("#people-edit-addaddress-menu-button-save").click
	(
		function()
		{
			url='model/ajax/people-edit-addaddress-save.php';
			$.post
			(
				url,
				{
					personid:$("#people-edit-value-id").val(),
					
					address1:$("#people-edit-value-address1").val(),
					address2:$("#people-edit-value-address2").val(),
					address3:$("#people-edit-value-address3").val(),
					county:$("#people-edit-value-county").val(),
					postcode:$("#people-edit-value-postcode").val(),
					
//					addressid:"addresqq"
				}
			);
			url='model/ajax/people-reload.php';
			selectedPeopleGroups = getSelectedPeopleGroups();
			$.post(
				url,
				{people:selectedPeopleGroups},
				onceLoadedPeopleSet);

			url='model/ajax/people-edit.php';
			$.post
			(
				url,
				{personID:$("#people-edit-value-id").val()},
				onceLoadedEditPerson
			);			
		}
	);
	
	
//	$("#people-edit-addaddress-menu-button-cancel").hover(function(){$(this).addClass("but-hover");},function(){$(this).removeClass("but-hover");});
	$("#people-edit-addaddress-menu-button-cancel").click(function(){$("#people-edit-addaddress").hide();});

	$("#people-edit-addaddress-save").hover(function(){$(this).addClass("but-hover");},function(){$(this).removeClass("but-hover");});
	$("#people-edit-addaddress-save").click
	(
		function()
		{
			$("#people-edit-addaddress").hide();
		}
	);

}

function getSelectedPeopleGroups()
{
//			alert("hi");
			var selectedPeopleGroups = new Array();
			var sql="";
//			$(this).toggleClass("li-selected");
			$(".li-selected").each
			(
				function()
				{
					groupID = $(this).attr("id");
					var start = groupID.lastIndexOf("_");
					groupNumber = groupID.slice(start+1);
					selectedPeopleGroups.push(groupNumber);
//					alert("groupID: "+groupID);
//					alert("groupnumber: "+groupNumber);
				}
			)
			return selectedPeopleGroups;
}
